/*
16 pots to MIDI CC
by Sebastian Tomczak
9 June 2007
Adelaide, Australia
*/

// command bytes
#include "WProgram.h"
void setup();
void loop();
byte status_byte = 176; // control change on channel 1
byte cc_number = 0; // value for control number

// init transfer and operation values
byte data_send = 0; // stores outgoing byte for spi bus

// init looping and working values
int workPort = 0; // working value for outgoing port
int workAn = 0; // working value for analog input

// setup begins here
void setup() {
Serial.begin(9600); // open serial port
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
}

// main program begins here
void loop() {

for(int i = 0; i <= 15; i ++) // loop the following code 8 times
{
cc_number = 20 + i; // set control number to between 20 and 25 depending on loop iteration
workPort = (i / 2) * 4; // get pot 1 to 8 ignoring bits 0 and 1 of a 5 bit value
PORTD = workPort; // set pins 2, 3 and 4 to value between 0 (00000) and 7 (11100)
workAn = i % 2; // and read in either analog input 0 or 1 depending on the loop
data_send = analogRead(workAn) / 8; //read analog pin into variable... with 10 bit -> 7 bit 

Serial.print("==============");
//Serial.print(status_byte,DEC); // send status byte

//Serial.print(cc_number,DEC); // send CC number
Serial.println(data_send,DEC); // send data value
//Serial.println(" ");
delay(150);
// loop back...
}
// loop back...
}

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

